<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.27">
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><script src="https://at.alicdn.com/t/font_2849934_v6y652peian.js"></script><link href="https://gitee.com/ran_yong/mark-down-table-upload/raw/master/img/20211123222242.png" rel="icon"><link rel="stylesheet" type="text/css" href="//at.alicdn.com/t/font_2970797_5eiedcvhf0a.css"><title>Go语言进阶学习 | Sakura</title><meta name="description" content="">
    <link rel="preload" href="/assets/js/runtime~app.5b846f34.js" as="script"><link rel="preload" href="/assets/css/styles.f5148030.css" as="style"><link rel="preload" href="/assets/js/452.9ec739fe.js" as="script"><link rel="preload" href="/assets/js/app.05eaf2e4.js" as="script">
    <link rel="stylesheet" href="/assets/css/styles.f5148030.css">
  </head>
  <body>
    <div id="app"><!--[--><!--[--><div class="common" style="--borderRadius:10px;--opacity:1;--fontColor:#2c3e50;--fontFamily:-apple-system;--fitter-blue:1px;"><header style="--borderRadius:10px;--opacity:1;--fontColor:#2c3e50;--fontFamily:-apple-system;--fitter-blue:1px;" class="header-bg-show navbar" id="c-navbar"><div class="toggle-sidebar-button"><svg class="icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div><span><a href="/" class=""><img style="border-radius:30px;" class="logo" src="https://gitee.com/ran_yong/mark-down-table-upload/raw/master/img/20211123222242.png" alt="Sakura"><span class="site-name" style="color: #2c3e50;">Sakura</span></a></span><div class="navbar-links-wrapper" style=""><!--[--><!--]--><nav class="navbar-links can-hide"><!--[--><div class="navbar-links-item"><!--[--><span class="aurora-nav-font aurora-navbar-icon"></span><a aria-current="page" href="/Back-end/Go%E8%AF%AD%E8%A8%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0.html#" class="router-link-active router-link-exact-active nav-link nav-link-active" aria-label="面经">面经</a><!--]--></div><div class="navbar-links-item"><div class="dropdown-wrapper"><span class="sakura-html aurora-nav-font aurora-navbar-icon"></span><button class="dropdown-title" type="button" aria-label="前端"><span class="title">前端</span><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="前端"><span class="title">前端</span><span class="right arrow"></span></button><ul style="display:none;" id="c-nav-dropdown" class="nav-dropdown"><!--[--><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>JavaScript</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Front-end/JavaScript%E8%AF%AD%E6%B3%95%E5%AD%A6%E4%B9%A0.html" class="nav-link" aria-label="JavaScript语言">JavaScript语言</a><!--]--></li><!--]--></ul><!--]--></li><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>TypeScript</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Front-end/TypeScript%E8%AF%AD%E6%B3%95%E5%AD%A6%E4%B9%A0.html" class="nav-link" aria-label="TS语法学习">TS语法学习</a><!--]--></li><!--]--></ul><!--]--></li><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>Vue项目实战</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Front-end/Vue%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98.html" class="nav-link" aria-label="Vue_shop 项目实战">Vue_shop 项目实战</a><!--]--></li><!--]--></ul><!--]--></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><span class="sakura-houduankaifa aurora-nav-font aurora-navbar-icon"></span><button class="dropdown-title" type="button" aria-label="后端"><span class="title">后端</span><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="后端"><span class="title">后端</span><span class="right arrow"></span></button><ul style="display:none;" id="c-nav-dropdown" class="nav-dropdown"><!--[--><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>Fastapi</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Back-end/Fastapi.html" class="nav-link" aria-label="Fastapi学习">Fastapi学习</a><!--]--></li><!--]--></ul><!--]--></li><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>Python基础</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Back-end/python%E5%9F%BA%E7%A1%80%E5%AD%A6%E4%B9%A0.html" class="nav-link" aria-label="python基础学习">python基础学习</a><!--]--></li><!--]--></ul><!--]--></li><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>Go基础</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Back-end/Go%E5%9F%BA%E7%A1%80%E5%AD%A6%E4%B9%A0.html" class="nav-link" aria-label="Go基础学习">Go基础学习</a><!--]--></li><!--]--></ul><!--]--></li><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>SQL基础</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Back-end/SQL%E5%9F%BA%E7%A1%80%E5%AD%A6%E4%B9%A0.html" class="nav-link" aria-label="SQL基础学习">SQL基础学习</a><!--]--></li><!--]--></ul><!--]--></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><span class="sakura-service aurora-nav-font aurora-navbar-icon"></span><button class="dropdown-title" type="button" aria-label="服务端"><span class="title">服务端</span><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="服务端"><span class="title">服务端</span><span class="right arrow"></span></button><ul style="display:none;" id="c-nav-dropdown" class="nav-dropdown"><!--[--><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>宝塔面板</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Other/WebHook.html" class="nav-link" aria-label="宝塔面板自动同步gitee仓库代码">宝塔面板自动同步gitee仓库代码</a><!--]--></li><!--]--></ul><!--]--></li><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>配置公钥</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Other/%E9%85%8D%E7%BD%AEgitee%E3%80%81github%E5%85%AC%E9%92%A5.html" class="nav-link" aria-label="配置gitee、github公钥">配置gitee、github公钥</a><!--]--></li><!--]--></ul><!--]--></li><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>宝塔配置HTTPS</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Other/%E5%AE%9D%E5%A1%94%E9%85%8D%E7%BD%AEHTTPS.html" class="nav-link" aria-label="如何通过宝塔配置HTTPS网站">如何通过宝塔配置HTTPS网站</a><!--]--></li><!--]--></ul><!--]--></li><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>Node后台运行</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Other/%E5%9C%A8linux%E4%B8%AD%E6%8C%81%E7%BB%AD%E8%BF%90%E8%A1%8CNode.js%E6%96%87%E4%BB%B6.html" class="nav-link" aria-label="在linux中持续运行Node.js文件">在linux中持续运行Node.js文件</a><!--]--></li><!--]--></ul><!--]--></li><li class="dropdown-item"><!--[--><h4 class="dropdown-subtitle"><span>Mac安装Scrcpy投屏软件</span></h4><ul class="dropdown-subitem-wrapper"><!--[--><li class="dropdown-subitem"><!--[--><!----><a href="/Other/Mac%E5%AE%89%E8%A3%85scrcpy%E6%8A%95%E5%B1%8F%E8%BD%AF%E4%BB%B6.html" class="nav-link" aria-label="Mac M1芯 安装scrcpy投屏软件">Mac M1芯 安装scrcpy投屏软件</a><!--]--></li><!--]--></ul><!--]--></li><!--]--></ul></div></div><div class="navbar-links-item"><!--[--><span class="sakura-shijianzhou aurora-nav-font aurora-navbar-icon"></span><a href="/aurora-archive/" class="nav-link nav-link-active" aria-label="时间轴">时间轴</a><!--]--></div><div class="navbar-links-item"><!--[--><span class="sakura-guanyuwomen aurora-nav-font aurora-navbar-icon"></span><a href="/about" class="nav-link nav-link-active" aria-label="关于我">关于我</a><!--]--></div><div class="navbar-links-item"><!--[--><span class="sakura-youlianguanli aurora-nav-font aurora-navbar-icon"></span><a href="/link" class="nav-link nav-link-active" aria-label="友链">友链</a><!--]--></div><div class="navbar-links-item"><!--[--><span class="sakura-biaoqian aurora-nav-font aurora-navbar-icon"></span><a href="/tag" class="nav-link nav-link-active" aria-label="标签">标签</a><!--]--></div><div class="navbar-links-item"><!--[--><span class="sakura-41shuoshuo aurora-nav-font aurora-navbar-icon"></span><a href="/mood" class="nav-link nav-link-active" aria-label="说说">说说</a><!--]--></div><div class="navbar-links-item"><!--[--><span class="sakura-xiangce aurora-nav-font aurora-navbar-icon"></span><a href="/photo" class="nav-link nav-link-active" aria-label="相册">相册</a><!--]--></div><div class="navbar-links-item"><!--[--><span class="sakura-github aurora-nav-font aurora-navbar-icon"></span><a class="nav-link external nav-link-active" href="https://github.com/ranyong1997/sakura-docs" rel="noopener noreferrer" target="_blank" aria-label="Sakura"><!--[--><!--]--> Sakura<!--[--><!--]--></a><!--]--></div><!--]--></nav><!--[--><!--]--><!----><form class="search-box" role="search"><input type="search" autocomplete="off" spellcheck="false" value><!----></form></div></header><!--[--><div id="" class="mobile-sidebar-control"></div><div id="" class="mobileAnimate mobile-sidebar"><!----></div><!--]--><div class="setColor" style="--borderRadius:10px;--fontFamily:-apple-system;--fontColor:#2c3e50;"><div id="setIco" class="setIco bg_color"></div><div class="welcome-parent" id="welcome-parent"><div class="custom-about m-4 p-4 f4 color-shadow-small bg-gray-800-mktg rounded-2 signup-content-container welcome" style="display: none;" id="welcome"><span class="cancel aurora-iconfont-common aurora-style-cancel" id="cancel"></span><h1 class="common-style" id="sr-only-h2"></h1><div class="custom-top custom-common"><div class="custom-top-span custom-common-span"><span class="aurora-iconfont-common aurora-style-img home-welcome-custom-icon"></span>  </div><div class="custom-top-span custom-common-span"><span class="aurora-iconfont-common aurora-style-blur home-welcome-custom-icon"></span>  </div></div><div class="custom-bottom custom-common"><div style="flex:1;" class="custom-bottom-span custom-common-span"><!--[--><li class="custom-li"><span style="background-color: #2c3e50;" data-color="#2c3e50"></span></li><li class="custom-li"><span style="background-color: #42a5f5;" data-color="#42a5f5"></span></li><li class="custom-li"><span style="background-color: #8093f1;" data-color="#8093f1"></span></li><li class="custom-li"><span style="background-color: #FF6EC7;" data-color="#FF6EC7"></span></li><li class="custom-li"><span style="background-color: #FF7F00;" data-color="#FF7F00"></span></li><li class="custom-li"><span style="background-color: #8FBC8F;" data-color="#8FBC8F"></span></li><li class="custom-li"><span style="background-color: #EAADEA;" data-color="#EAADEA"></span></li><li class="custom-li"><span style="background-color: #3299CC;" data-color="#3299CC"></span></li><!--]--></div></div><div class="custom-bottom custom-common"><div style="flex:1;" class="custom-bottom-span custom-common-span"><!--[--><li class="custom-li"><span style="font-family: -apple-system;" data-color="-apple-system">冉</span></li><li class="custom-li"><span style="font-family: hlt;" data-color="hlt">冉</span></li><li class="custom-li"><span style="font-family: tzt;" data-color="tzt">冉</span></li><li class="custom-li"><span style="font-family: sst;" data-color="sst">冉</span></li><li class="custom-li"><span style="font-family: lf;" data-color="lf">冉</span></li><li class="custom-li"><span style="font-family: xsf;" data-color="xsf">冉</span></li><li class="custom-li"><span style="font-family: lsf;" data-color="lsf">冉</span></li><li class="custom-li"><span style="font-family: cgt;" data-color="cgt">冉</span></li><!--]--></div></div><div class="custom-bottom custom-common"><div class="custom-bottom-span custom-common-span"> 圆角 </div><div style="flex:3;" class="custom-bottom-span custom-common-span"><div class="input-range"><div class="input-left"><input autocomplete min="1" max="40" value="10" step="1" type="range"></div><div class="input-right">10</div></div></div></div><div class="custom-bottom custom-common"><div class="custom-bottom-span custom-common-span"> 透明度 </div><div style="flex:3;" class="custom-bottom-span custom-common-span"><div class="input-range"><div class="input-left"><input autocomplete min="0" max="1" value="1" step="0.01" type="range"></div><div class="input-right">1</div></div></div></div></div></div></div><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><div style="--fontColor: #2c3e50; --fontFamily: -apple-system;" class="sidebar-single-enter-animate theme-container"><div class="page-sidebar"><div class="sidebar-single-enter-animate page-top" id="page-top" show-mood-edit="false"><div class="top-mask" style="background-image: url(https://api.ixiaowai.cn/api/api.php?time=233.9375);"></div><div class="aurora-top-bubble"><div class="aurora-top-bubble-par"><div class="aurora-top-bubble-box" id="aurora-top-bubble-box"></div><canvas class="aurora-top-bubble-canvas" id="aurora-top-bubble-canvas"></canvas></div></div><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><div class="page-record-control"><div class="page-top-record" id="page-top-record"><div class="page-record-bot-common page-record-top"><div class="page-record-top-left page-record-single-common"><span class="aurora-iconfont-common aurora-page-word"></span>  <span class="page-record-single-desc">总字数</span><span>0</span></div><div class="page-record-top-right page-record-single-common"><span class="aurora-iconfont-common aurora-page-time"></span>  <span class="page-record-single-desc">时长</span><span>0</span></div></div><div class="page-record-bot-common page-record-center"><div class="page-record-center-left page-record-single-common"><span class="aurora-iconfont-common aurora-page-comment"></span>  <span class="page-record-single-desc">评论数</span><span id="/Back-end/Go%E8%AF%AD%E8%A8%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0.html" class="waline-comment-count"></span></div><div class="page-record-center-right page-record-single-common"><span class="aurora-iconfont-common aurora-page-read"></span>  <span class="page-record-single-desc">总阅读数</span><span id="/Back-end/Go%E8%AF%AD%E8%A8%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0.html" class="waline-visitor-count"></span></div></div><!----></div></div><div class="top-image" id="top-image"><h1></h1></div></div><div class="content"><div id="article-page-parent" class="article-page-parent-pro article-page-parent"><div class="noShowSidebar page-sidebar-left" id="page-sidebar-left"><!--[--><!--]--><!--[--><!--[--><main style="--borderRadius: 10px;--opacity: 1;" class="page sidebar-single-enter-animate" id="article-page"><!--[--><!--]--><div id="theme-default-content" class="hide-h1-tag theme-default-content pageContent medium-zoom-content"><div class="page-top-share"><div class="page-top-share-next"><div class="poster-share" style="z-index:99;"><div class="poster-button" id="poster-button"><span class="aurora-iconfont-common aurora-poster-ico" data="1">生成海报3</span>  </div></div></div></div><!--[--><h1 id="go语言进阶学习" tabindex="-1"><a class="header-anchor" href="#go语言进阶学习" aria-hidden="true">#</a> Go语言进阶学习</h1><h2 id="写在前面" tabindex="-1"><a class="header-anchor" href="#写在前面" aria-hidden="true">#</a> 写在前面：</h2><blockquote><p>Go是一门编译型语言，Go语言的工具链将源代码及其依赖转换成计算机的机器指令。Go语言提供的工具都通过一个单独的命令<code>go</code>调用，<code>go</code>命令有一系列的子命令。最简单的一个子命令就是<code>run</code>。这个命令编译一个或多个以.go结尾的源文件。</p></blockquote><h2 id="hello-world" tabindex="-1"><a class="header-anchor" href="#hello-world" aria-hidden="true">#</a> Hello,World</h2><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Hello, 世界&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>编译：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> run helloworld<span class="token punctuation">.</span><span class="token keyword">go</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><p>输出：</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>Hello, 世界
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><blockquote><p>如果你希望能够编译这个程序，保存编译后即过以备将来之用。可以用build子命令。</p></blockquote><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> build helloworld<span class="token punctuation">.</span><span class="token keyword">go</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><blockquote><p>这个命令生成一个以helloworld的课执行的二进制文件，之后可以随时运行它。不需要任何处理。</p></blockquote><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>./helloworld
Hello, 世界
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="custom-container tip"><p class="custom-container-title">TIP</p><p>Go的标准库提供了100多个包，以支持常见功能，比如输入、输出、排序以及文本处理。比如<code>fmt</code>包，就含有格式化输出、接收输入的函数。<code>println</code>是其中的一个基础函数，可以打印以空格间隔的一个或多个值，并在最后添加一个换行符，从而输出一整行。</p><p><code>main</code>包比较特殊。它定义了一个独立的可执行的成宿，而不是一个库。在<code>main</code>里面的<code>main</code>函数也很特殊，它是整个程序执行的入口。<code>main</code>函数所做的事情就是程序做的。当然，<code>main</code>函数一般调用其它包里的函数完成很多工作。比如<code>fmt.Println</code>。</p><p>必须告诉编译器源文件需要哪些包，这就是<code>import</code>声明以及随后的<code>package</code>声明扮演的角色。</p><p>Go语言不需要在语句或者声明的末尾添加分号，排除一行上有多条语句。</p></div><h3 id="命令行参数" tabindex="-1"><a class="header-anchor" href="#命令行参数" aria-hidden="true">#</a> 命令行参数</h3><blockquote><p>大多数程序都是处理输入，参数输出。</p></blockquote><p><code>os</code>包以跨平台的方式，提供了一些与操作系统交互的函数和变量。程序的命令行参数可从<code>os</code>包的<code>Args</code>量获取；<code>os</code>包外部使用<code>os.Args</code>访问该变量。</p><p><code>os.Args</code>变量是一个字符串的切片，<code>os.Args</code>的第一个元素，<code>os.Args[0]</code>,是命令本身的名字；其它的元素则是程序启动时传给它的参数。<code>s[m:n]</code>形式的切片表达式，产生从第<code>m</code>个元素到第<code>n-1</code>个元素的切片;如果省略切片表达式的m或n，会默认传入0或len(s)，因此前面的切片可以简写成<code>os.Args[1:]</code></p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;os&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">var</span> s<span class="token punctuation">,</span> sep <span class="token builtin">string</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token function">len</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>Args<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
		s <span class="token operator">+=</span> sep <span class="token operator">+</span> os<span class="token punctuation">.</span>Args<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
		sep <span class="token operator">=</span> <span class="token string">&quot; &quot;</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><div class="custom-container tip"><p class="custom-container-title">TIP</p><p>注释语句以<code>//</code>开头。</p><p>符号 := 是短变量声明</p></div><p>Go语言只有for循环着一种循环语句</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">for</span> initialization<span class="token punctuation">;</span> condition<span class="token punctuation">;</span> post<span class="token punctuation">{</span>
	<span class="token comment">// 语句</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="custom-container tip"><p class="custom-container-title">TIP</p><p>for循环三个部分部需要括号包围。大括号强制要求，左大括号必须和<code>post</code>语句在同一行。</p><p><code>initialization</code>语句是可选的，在循环开始前执行。<code>initialization</code>如果存在，必须是一条简单语句。即，短变量声明、自增语句、赋值语句或函数调用。</p><p><code>condition</code>是一个布尔表达式，其值在每次循环迭代开始时计算。如果为<code>true</code>则执行循环体语句。</p><p><code>post</code>语句在循环体执行结束后执行，之后再次对<code>condition</code>求值。<code>condition</code>为<code>false</code>时，循环结束。</p></div><p>for循环的三个部分每个都可以省略，如果省略<code>initialization</code>和<code>post</code>，分号也可以省略:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 传统的while循环</span>
<span class="token keyword">for</span> condition <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果连<code>condition</code>也省略了</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 传统的无限循环</span>
<span class="token keyword">for</span> <span class="token punctuation">{</span>
	<span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><blockquote><p>这就变成了一个无限循环，尽管如此。还可以用其他的方式终止循环，如一条<code>break</code>或<code>return</code>语句。</p></blockquote><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;os&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	s<span class="token punctuation">,</span> sep <span class="token operator">:=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> arg <span class="token operator">:=</span> <span class="token keyword">range</span> os<span class="token punctuation">.</span>Args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token punctuation">{</span>
		s <span class="token operator">+=</span> sep <span class="token operator">+</span> arg
		sep <span class="token operator">=</span> <span class="token string">&quot; &quot;</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><div class="custom-container tip"><p class="custom-container-title">TIP</p><p>Go语言不允许使用无用的局部变量，因为会导致编译错误。</p><p>Go语言中这种情况的解决方法是用<code>空标识符</code>，即<code>_</code>。空标识符可用于任何语法需要变量但程序逻辑不需要的时候，例如：在循环里，丢弃不需要的循环索引，保留元素值。</p></div><p>请看下列两种变量声明</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>s <span class="token operator">:=</span> <span class="token string">&quot;&quot;</span>
<span class="token keyword">var</span> s <span class="token builtin">string</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><blockquote><p>第一种形式，是一条短变量的生命，最简洁，但只能用于在函数内部。而不能用于包变量。</p><p>第二种形式依赖于字符串的默认初始化零值机制，被初始化为&quot;&quot;。</p></blockquote><h3 id="查找重复的行" tabindex="-1"><a class="header-anchor" href="#查找重复的行" aria-hidden="true">#</a> 查找重复的行</h3><blockquote><p>对文件做拷贝、打印、搜索、排序、统计或类似事情的程序都有一个差不多的程序结构：一个处理输入的循环，在每个元素上执行计算处理，在处理的同时或最后产生输出。</p></blockquote><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;bufio&quot;</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;os&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	counts <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">)</span>
	input <span class="token operator">:=</span> bufio<span class="token punctuation">.</span><span class="token function">NewScanner</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>Stdin<span class="token punctuation">)</span>
	<span class="token keyword">for</span> input<span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		counts<span class="token punctuation">[</span>input<span class="token punctuation">.</span><span class="token function">Text</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token operator">++</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">for</span> line<span class="token punctuation">,</span> n <span class="token operator">:=</span> <span class="token keyword">range</span> counts <span class="token punctuation">{</span>
		<span class="token keyword">if</span> n <span class="token operator">&gt;</span> <span class="token number">1</span> <span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\t%s\n&quot;</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> line<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><blockquote><p>正如<code>for</code>循环一样，<code>if</code>语句条件两边也不加括号，但是主体部分需要家。<code>if</code>语句的<code>else</code>部分时可选的，在<code>if</code>的条件为<code>false</code>时执行。</p></blockquote><div class="custom-container tip"><p class="custom-container-title">TIP</p><p><code>map</code>存储了键/值的集合，对集合元素，提供常数时间的存、取或测试操作。键可以时任意类型，只要其值能用<code>==</code>运算符比较，最常见的例子是字符串；值则可以是任意类型。</p><p>每次<code>dup</code>读取一行输入，该行被当作<code>map</code>,其对应的值递增。</p><p><code>counts[input.Text()]++</code>语句等价于：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>line <span class="token operator">:=</span> input<span class="token punctuation">.</span><span class="token function">Text</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
counts<span class="token punctuation">[</span>line<span class="token punctuation">]</span> <span class="token operator">=</span> counts<span class="token punctuation">[</span>line<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">1</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>map</code>中不含某个键时不用担心，首次读到新行时，等号右边的表达式的值将被计算为其类型的零值，对于<code>int</code>即0。</p><p>继续来看<code>bufio</code>包，它使处理输入和输出方便又高效。<code>Scanner</code>类型时该包最有用的特性之一,它读取输入并将其拆成行或者单词；通常时处理形式的输入最简单的方法。</p><p>程序使用短变量声明创建<code>bufio.Scanner</code>类型的变量<code>input</code>。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>input <span class="token operator">:=</span> bufio<span class="token punctuation">.</span><span class="token function">NewScanner</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>Stdin<span class="token punctuation">)</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><p>该变量从程序的标准输入中读取内容。每次调用<code>input.Scanner</code>,即读入下一行，并移除行末的换行符；读取的内容可以调用<code>input.Text()</code>得到。<code>Scan</code>函数在读到一行时返回<code>true</code>,在无输入时返回<code>false</code>。</p><p><code>fmt.Printf</code>函数对一些表达式产生格式化输出。该函数的首个参数时格式字符串，指定后续参数被如何格式化。各个参数的格式取决于“转换字符”，形式为百分号后跟一个字母。</p></div><table><thead><tr><th>%d</th><th>十进制整数</th></tr></thead><tbody><tr><td>&amp;x，%o，%b</td><td>十六进制，八进制，二进制整数</td></tr><tr><td>%f，%g，%e</td><td>单元格</td></tr><tr><td>%t</td><td>布尔值</td></tr><tr><td>%c</td><td>字符</td></tr><tr><td>%s</td><td>字符串</td></tr><tr><td>%q</td><td>带双引号的字符串&quot;abc&quot;或带单引号的字符&#39;c&#39;</td></tr><tr><td>%v</td><td>变量的自然形式</td></tr><tr><td>%T</td><td>变量的类型</td></tr><tr><td>%%</td><td>字面上的百分号标志</td></tr></tbody></table><p><code>dup1</code>的格式字符串中还有制表符<code>\t</code>和换行符<code>\n</code>。字符串字面上可能含有这些代表不可见字符的转义字符。默认情况下，<code>Printf</code>不会换行。以字母<code>f</code>结尾的格式化函数。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main
<span class="token keyword">import</span> <span class="token punctuation">(</span>
    <span class="token string">&quot;bufio&quot;</span>
    <span class="token string">&quot;fmt&quot;</span>
    <span class="token string">&quot;os&quot;</span>
<span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    counts <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">)</span>
    files <span class="token operator">:=</span> os<span class="token punctuation">.</span>Args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
    <span class="token keyword">if</span> <span class="token function">len</span><span class="token punctuation">(</span>files<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
        <span class="token function">countLines</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>Stdin<span class="token punctuation">,</span> counts<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> arg <span class="token operator">:=</span> <span class="token keyword">range</span> files <span class="token punctuation">{</span>
            f<span class="token punctuation">,</span> err <span class="token operator">:=</span> os<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span>arg<span class="token punctuation">)</span>
            <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span> <span class="token punctuation">}</span>
    fmt<span class="token punctuation">.</span><span class="token function">Fprintf</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>Stderr<span class="token punctuation">,</span> <span class="token string">&quot;dup2: %v\n&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
<span class="token keyword">continue</span>
<span class="token punctuation">}</span>
<span class="token function">countLines</span><span class="token punctuation">(</span>f<span class="token punctuation">,</span> counts<span class="token punctuation">)</span>
f<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> line<span class="token punctuation">,</span> n <span class="token operator">:=</span> <span class="token keyword">range</span> counts <span class="token punctuation">{</span>
        <span class="token keyword">if</span> n <span class="token operator">&gt;</span> <span class="token number">1</span> <span class="token punctuation">{</span>
            fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\t%s\n&quot;</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> line<span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
<span class="token punctuation">}</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">countLines</span><span class="token punctuation">(</span>f <span class="token operator">*</span>os<span class="token punctuation">.</span>File<span class="token punctuation">,</span> counts <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    input <span class="token operator">:=</span> bufio<span class="token punctuation">.</span><span class="token function">NewScanner</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span>
    <span class="token keyword">for</span> input<span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        counts<span class="token punctuation">[</span>input<span class="token punctuation">.</span><span class="token function">Text</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token operator">++</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><p><code>os.Open</code>函数返回两个值。第一个值是被打开的文件(<code>*os.File</code>),其后被<code>Scanner</code>读取。</p><p><code>os.Open</code>返回第二个值是内置<code>error</code>类型的值。如果<code>err</code>等于内置<code>nil</code>,那么文件被成功打开。读取文件，直到文件结束，然后调用<code>Close</code>关闭该文件，并释放占用的所有资源。相反的话，如果<code>err</code>的值不为<code>nil</code>，说明打开文件时出错了。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;io/ioutil&quot;</span>
	<span class="token string">&quot;os&quot;</span>
	<span class="token string">&quot;strings&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	counts <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> filename <span class="token operator">:=</span> <span class="token keyword">range</span> os<span class="token punctuation">.</span>Args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token punctuation">{</span>
		data<span class="token punctuation">,</span> err <span class="token operator">:=</span> ioutil<span class="token punctuation">.</span><span class="token function">ReadFile</span><span class="token punctuation">(</span>filename<span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Fprintf</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>Stderr<span class="token punctuation">,</span> <span class="token string">&quot;dup3: %v\n&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
			<span class="token keyword">continue</span>
		<span class="token punctuation">}</span>
		<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> line <span class="token operator">:=</span> <span class="token keyword">range</span> strings<span class="token punctuation">.</span><span class="token function">Split</span><span class="token punctuation">(</span><span class="token function">string</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;\n&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			counts<span class="token punctuation">[</span>line<span class="token punctuation">]</span><span class="token operator">++</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">for</span> line<span class="token punctuation">,</span> n <span class="token operator">:=</span> <span class="token keyword">range</span> counts <span class="token punctuation">{</span>
		<span class="token keyword">if</span> n <span class="token operator">&gt;</span> <span class="token number">1</span> <span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\t%s\n&quot;</span><span class="token punctuation">,</span> n<span class="token punctuation">,</span> line<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><p><code>ReadFile</code>函数返回一个字节切片，必须把它转换为<code>string</code>,才能用<code>strings.Split</code>分割。</p><h3 id="获取url" tabindex="-1"><a class="header-anchor" href="#获取url" aria-hidden="true">#</a> 获取URL</h3><blockquote><p>Go语言在net这个强大的package的帮助下提供了一系列的package来做这件事，使用这些包可以更简明的用网络收发信息，还可以建立更底层的网络连接，编写服务器程序。</p></blockquote><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;io/ioutil&quot;</span>
	<span class="token string">&quot;net/http&quot;</span>
	<span class="token string">&quot;os&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> url <span class="token operator">:=</span> <span class="token keyword">range</span> os<span class="token punctuation">.</span>Args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token punctuation">{</span>
		resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> http<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Fprintf</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>Stderr<span class="token punctuation">,</span> <span class="token string">&quot;fetch: %v\n&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
			os<span class="token punctuation">.</span><span class="token function">Exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		b<span class="token punctuation">,</span> err <span class="token operator">:=</span> ioutil<span class="token punctuation">.</span><span class="token function">ReadAll</span><span class="token punctuation">(</span>resp<span class="token punctuation">.</span>Body<span class="token punctuation">)</span>
		resp<span class="token punctuation">.</span>Body<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Fprintf</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>Stderr<span class="token punctuation">,</span> <span class="token string">&quot;fetch: reading %s: %v\n&quot;</span><span class="token punctuation">,</span> url<span class="token punctuation">,</span> err<span class="token punctuation">)</span>
			os<span class="token punctuation">.</span><span class="token function">Exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
		fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s&quot;</span><span class="token punctuation">,</span> b<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>这个程序从葱两个package中导入了函数，<code>net/http</code>和<code>io/ioutil</code>包</p><p><code>http.Get</code>函数是创建<code>HTTP</code>请求的函数，如果获取过程没有出错，那么会在<code>resp</code>这个结构体中得到访问的请求结果。<code>resp</code>的<code>Body</code>字段包括一个可读服务器响应流。<code>ioutill.ReadAll</code>函数从<code>response</code>中读取到全部内容；将其结果保存到变量b中。<code>resp.Body.Close</code>关闭<code>resp</code>的<code>Body</code>流，防止资源泄漏，Printf函数会将结果b写出到标准输出流中。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> run url<span class="token punctuation">.</span><span class="token keyword">go</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>baidu<span class="token punctuation">.</span>com
</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><p><img src="https://ooszy.cco.vin/img/blog-public/ljz.gif" alt="" originSrc="https://gitee.com/ran_yong/mark-down-table-upload/raw/master/img/image-20211225104934220.png" data="aurora"></p><p>HTTP请求如果失败的话，会得到下面这样的结果：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> run url<span class="token punctuation">.</span><span class="token keyword">go</span> htttps<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>sakura<span class="token punctuation">.</span>daydayupran<span class="token punctuation">.</span>top234<span class="token operator">/</span>

fetch<span class="token punctuation">:</span> Get <span class="token string">&quot;https://sakura.daydayupran.top234/&quot;</span><span class="token punctuation">:</span> dial tcp<span class="token punctuation">:</span> lookup sakura<span class="token punctuation">.</span>daydayupran<span class="token punctuation">.</span>top234<span class="token punctuation">:</span> no such host
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>无论哪种失败，我们的程序都用了os.Exit函数来终止进程，并且返回一个status错误码，其值为1。</p><h3 id="并发获取多个url" tabindex="-1"><a class="header-anchor" href="#并发获取多个url" aria-hidden="true">#</a> 并发获取多个URL</h3><blockquote><p>Go语言最有意思并且最新奇的特性就是对并发编程的支持。</p></blockquote><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;io&quot;</span>
	<span class="token string">&quot;io/ioutil&quot;</span>
	<span class="token string">&quot;net/http&quot;</span>
	<span class="token string">&quot;os&quot;</span>
	<span class="token string">&quot;time&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	start <span class="token operator">:=</span> time<span class="token punctuation">.</span><span class="token function">Now</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	ch <span class="token operator">:=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token keyword">chan</span> <span class="token builtin">string</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> url <span class="token operator">:=</span> <span class="token keyword">range</span> os<span class="token punctuation">.</span>Args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token punctuation">{</span>
		<span class="token keyword">go</span> <span class="token function">fetch</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> ch<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">for</span> <span class="token keyword">range</span> os<span class="token punctuation">.</span>Args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token operator">&lt;-</span>ch<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;%.2fs elapsed\n&quot;</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span><span class="token function">Since</span><span class="token punctuation">(</span>start<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Seconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">fetch</span><span class="token punctuation">(</span>url <span class="token builtin">string</span><span class="token punctuation">,</span> ch <span class="token keyword">chan</span><span class="token operator">&lt;-</span> <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	start <span class="token operator">:=</span> time<span class="token punctuation">.</span><span class="token function">Now</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> http<span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		ch <span class="token operator">&lt;-</span> fmt<span class="token punctuation">.</span><span class="token function">Sprint</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	nbytes<span class="token punctuation">,</span> err <span class="token operator">:=</span> io<span class="token punctuation">.</span><span class="token function">Copy</span><span class="token punctuation">(</span>ioutil<span class="token punctuation">.</span>Discard<span class="token punctuation">,</span> resp<span class="token punctuation">.</span>Body<span class="token punctuation">)</span>
	resp<span class="token punctuation">.</span>Body<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		ch <span class="token operator">&lt;-</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">&quot;while reading %s:%v&quot;</span><span class="token punctuation">,</span> url<span class="token punctuation">,</span> err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	secs <span class="token operator">:=</span> time<span class="token punctuation">.</span><span class="token function">Since</span><span class="token punctuation">(</span>start<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Seconds</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	ch <span class="token operator">&lt;-</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">&quot;%.2fs %7d %s&quot;</span><span class="token punctuation">,</span> secs<span class="token punctuation">,</span> nbytes<span class="token punctuation">,</span> url<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><p>下面使用fetchall来请求多个地址:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> run fechall<span class="token punctuation">.</span><span class="token keyword">go</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>hao123<span class="token punctuation">.</span>com http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>baidu<span class="token punctuation">.</span>com

<span class="token number">0.06</span>s  <span class="token number">329011</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>baidu<span class="token punctuation">.</span>com
<span class="token number">0.47</span>s  <span class="token number">450657</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>hao123<span class="token punctuation">.</span>com
<span class="token number">0.47</span>s elapsed
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><code>goroutine</code>是一种函数的并发执行方式，而<code>channel</code>是用来在<code>goroutine</code>之间进行参数传递。</p><h3 id="web服务" tabindex="-1"><a class="header-anchor" href="#web服务" aria-hidden="true">#</a> Web服务</h3><blockquote><p>Go语言的内置库使用写一个类似fetch的web服务器变得异常地简单</p></blockquote><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// Server1 is a minimal &quot;echo&quot; server.</span>
<span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;log&quot;</span>
	<span class="token string">&quot;net/http&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	http<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	log<span class="token punctuation">.</span><span class="token function">Fatal</span><span class="token punctuation">(</span>http<span class="token punctuation">.</span><span class="token function">ListenAndServe</span><span class="token punctuation">(</span><span class="token string">&quot;localhost:8000&quot;</span><span class="token punctuation">,</span> <span class="token boolean">nil</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">handler</span><span class="token punctuation">(</span>w http<span class="token punctuation">.</span>ResponseWriter<span class="token punctuation">,</span> r <span class="token operator">*</span>http<span class="token punctuation">.</span>Request<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Fprintf</span><span class="token punctuation">(</span>w<span class="token punctuation">,</span> <span class="token string">&quot;URL.Path = %q\n&quot;</span><span class="token punctuation">,</span> r<span class="token punctuation">.</span>URL<span class="token punctuation">.</span>Path<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>main函数将所有发送到/路径下的请求和handler函数关联起来，/开头的请求其实就是所有发送到当前站点上的请求，服务监听8000端口。发送到这个服务的&quot;请求&quot;是一个http.Request类型的对象，这个对象中包含了请求中的一系列相关字段，其中就包括我们需要的URL。</p><p><img src="https://ooszy.cco.vin/img/blog-public/ljz.gif" alt="" originSrc="https://gitee.com/ran_yong/mark-down-table-upload/raw/master/img/image-20211225173741395.png" data="aurora"></p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;log&quot;</span>
	<span class="token string">&quot;net/http&quot;</span>
	<span class="token string">&quot;sync&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">var</span> mu sync<span class="token punctuation">.</span>Mutex
<span class="token keyword">var</span> count <span class="token builtin">int</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	http<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> handler<span class="token punctuation">)</span>
	http<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">&quot;/count&quot;</span><span class="token punctuation">,</span> counter<span class="token punctuation">)</span>
	log<span class="token punctuation">.</span><span class="token function">Fatal</span><span class="token punctuation">(</span>http<span class="token punctuation">.</span><span class="token function">ListenAndServe</span><span class="token punctuation">(</span><span class="token string">&quot;localhost:8000&quot;</span><span class="token punctuation">,</span> <span class="token boolean">nil</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">handler</span><span class="token punctuation">(</span>w http<span class="token punctuation">.</span>ResponseWriter<span class="token punctuation">,</span> r <span class="token operator">*</span>http<span class="token punctuation">.</span>Request<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	mu<span class="token punctuation">.</span><span class="token function">Lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	count<span class="token operator">++</span>
	mu<span class="token punctuation">.</span><span class="token function">Unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Fprintf</span><span class="token punctuation">(</span>w<span class="token punctuation">,</span> <span class="token string">&quot;URL.Path=%q\n&quot;</span><span class="token punctuation">,</span> r<span class="token punctuation">.</span>URL<span class="token punctuation">.</span>Path<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">counter</span><span class="token punctuation">(</span>w http<span class="token punctuation">.</span>ResponseWriter<span class="token punctuation">,</span> r <span class="token operator">*</span>http<span class="token punctuation">.</span>Request<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	mu<span class="token punctuation">.</span><span class="token function">Lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Fprintf</span><span class="token punctuation">(</span>w<span class="token punctuation">,</span> <span class="token string">&quot;Count %d\n&quot;</span><span class="token punctuation">,</span> count<span class="token punctuation">)</span>
	mu<span class="token punctuation">.</span><span class="token function">Unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><p>这个服务器有两个请求处理函数，根据请求的url不同会调用不同的函数：对/count这个url的请求会调用到count这个函数，其它url都会调用默认的处理函数。如果你的请求pattern是以/结尾，那么所有以该url为前缀的url都会被这条规则匹配。</p><h2 id="程序结构" tabindex="-1"><a class="header-anchor" href="#程序结构" aria-hidden="true">#</a> 程序结构</h2><blockquote><p>Go语法和其他结构语言一样样，一个大的程序由很多小的基础构件组成。变量保存值，简单的加法和减法运算被组合成较复杂的表达式。</p></blockquote><h3 id="命名" tabindex="-1"><a class="header-anchor" href="#命名" aria-hidden="true">#</a> 命名</h3><blockquote><p>Go语言中的函数名、变量名、常量名、类型名、语句标号和包名等所有的命名，都遵循一个简单的命名规则：一个名字必须以一个字母或下划线开头，后面可以跟任何数量的字母、数字或下划线。大写字母和小写字母是不同的。</p></blockquote><p>Go语言中类似if和switch的关键字有25个；关键字不能用于自定义名字，只能在特定语法结构中使用。</p><table><thead><tr><th>break</th><th>default</th><th>func</th><th>interface</th><th>Select</th></tr></thead><tbody><tr><td>case</td><td>defer</td><td>go</td><td>map</td><td>struct</td></tr><tr><td>chan</td><td>else</td><td>goto</td><td>package</td><td>switch</td></tr><tr><td>const</td><td>fallthrough</td><td>if</td><td>range</td><td>type</td></tr><tr><td>continue</td><td>for</td><td>import</td><td>return</td><td>Var</td></tr></tbody></table><p>Go语言推荐使用驼峰命名，当名字又几个单词组成的生活优先使用大小写分隔，而不是优先使用下划线分隔。</p><h3 id="声明" tabindex="-1"><a class="header-anchor" href="#声明" aria-hidden="true">#</a> 声明</h3><blockquote><p>声明语句定义了程序的各大实体对象以及部分或全部的属性。Go语言主要由四种类型的声明语句：var、const、type和func，分别对应变量、常量、类型和函数实体对象的声明。</p></blockquote><p>一个Go语言编写的程序对应一个或多个以go为文件后缀名的源文件中。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token string">&quot;fmt&quot;</span>

<span class="token keyword">const</span> boilingF <span class="token operator">=</span> <span class="token number">212.0</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">var</span> f <span class="token operator">=</span> boilingF
	<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token punctuation">(</span>f <span class="token operator">-</span> <span class="token number">32</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">5</span> <span class="token operator">/</span> <span class="token number">9</span>
	fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;boiling point = %g°F or %g°C\\n&quot;</span><span class="token punctuation">,</span> f<span class="token punctuation">,</span> c<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>其中常量boilingF是在包一级范围声明语句声明的，然后f和c两个变量是在main函数内部</p><!--]--></div><footer class="page-meta"><div class="meta-item edit-link"><!--[--><span class="aurora-nav-font aurora-navbar-icon"></span><a class="nav-link external nav-link-active meta-item-label" href="https://github.com/ranyong1997/sakura-docs/edit/main/docs/Back-end/Go语言进阶学习.md" rel="noopener noreferrer" target="_blank" aria-label="编辑"><!--[--><!--]--> 编辑<!--[--><!--]--></a><!--]--></div><div class="meta-item last-updated"><span class="meta-item-label">lastTime: </span><span class="meta-item-info">2022/1/6 上午12:04:32</span></div><div class="meta-item contributors"><span class="meta-item-label">Contributors: </span><span class="meta-item-info"><!--[--><!--[--><span class="contributor" title="email: 67427627+ranyong1997@users.noreply.github.com">luck_baby</span><!----><!--]--><!--]--></span></div></footer><!--[--><!--]--></main><main style="--borderRadius:10px;--opacity:1;" class="page box"><div class="theme-default-content"><!--[--><div style="--homePageImgApi:url(https://api.ixiaowai.cn/api/api.php?time=-45377.28571428571);" class="page-bottom-next"><div class="page-next-left page-next-item"><a aria-current="page" href="/Back-end/Go%E8%AF%AD%E8%A8%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0.html" class="router-link-active router-link-exact-active"><span></span></a></div><!----></div><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--></div></main><div><!----></div><div class="recommend-page"><main style="--borderRadius:10px;--opacity:1;" class="page box"><h2 id="recommend-top-title">推荐阅读</h2><div id="recommend" class="recommend"><!--[--><!--]--></div></main></div><div id="control-comment" class="aurora-control-comment-box"><div style="--borderRadius: 10px;--opacity: 1;" class="theme-comment-box"><span class="aurora-comment-common aurora-iconfont-common page-comment-icon"></span><span class="aurora-comment-common aurora-comment-text">点击评论</span></div><div class="mobile-record aurora-comment-animate"><div class="page box" style="--borderRadius: 10px;--opacity: 1;"><div id="waline" class="vcomment-bottom theme-default-content"></div></div></div></div><!--]--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--></div><div id="page-sidebar-right" style="" class="page-sidebar-right"><div class="stickSidebar"><!--[--><div class="sidebar-cqy"></div><div style="--borderRadius: 10px;--opacity: 1;" id="stickSidebar" class="sidebar-single-enter-animate"><div class="sidebarScroll home-sidebar" style="" id="home-sidebar"><div id="" class="sidebar-single-common"><div class="home-sidebar-avatar"><img id="home-sidebar-avatar-img" src="https://gitee.com/ran_yong/mark-down-table-upload/raw/master/img/20211123222242.png" alt=""></div><div class="home-sidebar-info-desc"><span>Sakura</span></div><div class="home-sidebar-info-page"><div class="sidebar-page-common"><div>文章</div><div>19</div></div><div class="sidebar-page-common"><div>标签</div><div>24</div></div><div class="sidebar-page-common"><div>类别</div><div>4</div></div></div><a target="_blank" href="https://github.com/ranyong1997/sakura-docs"><div class="sidebar-github">Github</div></a><!--[--><!--]--></div><!--[--><!--]--><!----><!--[--><!--]--><div id="" class="sidebar-single-common"><div class="sidebar-link"><!--[--><!--]--></div></div><!----><!--[--><!--]--><!----><!--[--><!--]--><div id="" class="sidebar-single-enter-animate sidebar-single-page sidebar-single-common"><div class="change-page"><div class="catalog-page change-page-common"><span class="changePageActive" index="1">文章目录</span></div><div style="" class="latest-page change-page-common"><!----><span style="" class="" index="2">最新文章</span></div></div><div class="" style=""><!--[--><!--]--></div><div style="display:none;"><!--[--><!--]--></div><!--[--><!--]--></div><!--[--><!--]--><div id="" class="sidebar-single-enter-animate sidebar-single-common"><div class="sidebar-page"><span class="aurora-iconfont-common aurora-sidebar-message"></span><span>公告</span></div><div class="sidebar-message"><!--[--><li id="sidebar-message" class="sidebar-hover-bg-common"><span>这是一个公告<a href="#">Sakura</a></span></li><li id="sidebar-message" class="sidebar-hover-bg-common"><span>祝大家学业进步</span></li><!--]--></div><!--[--><!--]--></div><!--[--><!--]--><div id="" class="sidebar-single-enter-animate sidebar-single-common"><div id="home-tag-sidebar" class="sidebar-page"><span class="aurora-iconfont-common aurora-sidebar-tag"></span><span>标签</span></div><!----><div class="sidebar-tag-item"><!--[--><a href="/tag?tag=Python" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #aed9e0; font-size: 15px;">Python</span></div></a><a href="/tag?tag=后端" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #84c7d0; font-size: 18px;">后端</span></div></a><a href="/tag?tag=接口" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #a9def9; font-size: 34px;">接口</span></div></a><a href="/tag?tag=Go" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #ef90b3; font-size: 32px;">Go</span></div></a><a href="/tag?tag=数据库" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #ff9800; font-size: 29px;">数据库</span></div></a><a href="/tag?tag=SQL" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #fbc4ab; font-size: 29px;">SQL</span></div></a><a href="/tag?tag=Python基础" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #a9def9; font-size: 33px;">Python基础</span></div></a><a href="/tag?tag=前端三剑客" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #72ddf7; font-size: 18px;">前端三剑客</span></div></a><a href="/tag?tag=JavaScript" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #6d45bb; font-size: 14px;">JavaScript</span></div></a><a href="/tag?tag=TypeScript" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #84c7d0; font-size: 23px;">TypeScript</span></div></a><a href="/tag?tag=Vue" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #42bfdd; font-size: 27px;">Vue</span></div></a><a href="/tag?tag=Element UI" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #26c6da; font-size: 34px;">Element UI</span></div></a><a href="/tag?tag=Axios" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #a9def9; font-size: 11px;">Axios</span></div></a><a href="/tag?tag=Scrcpy" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #a9def9; font-size: 22px;">Scrcpy</span></div></a><a href="/tag?tag=投屏软件" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #62b6cb; font-size: 25px;">投屏软件</span></div></a><a href="/tag?tag=宝塔面板" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #b8f2e6; font-size: 32px;">宝塔面板</span></div></a><a href="/tag?tag=同步代码" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #aed9e0; font-size: 23px;">同步代码</span></div></a><a href="/tag?tag=Node" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #ffcad4; font-size: 16px;">Node</span></div></a><a href="/tag?tag=持续运行" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #ffcad4; font-size: 27px;">持续运行</span></div></a><a href="/tag?tag=HTTPS" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #fbc4ab; font-size: 16px;">HTTPS</span></div></a><a href="/tag?tag=SSL" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #5b5f97; font-size: 24px;">SSL</span></div></a><a href="/tag?tag=Github" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #b39ddb; font-size: 23px;">Github</span></div></a><a href="/tag?tag=Gitee" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #42bfdd; font-size: 33px;">Gitee</span></div></a><a href="/tag?tag=公钥" class=""><div class="sidebar-tag-single"><span class="home-sidebar-tag-hover" style="color: #1565c0; font-size: 20px;">公钥</span></div></a><!--]--></div><!--[--><!--]--></div><!--[--><!--]--><!----><!--[--><!--]--></div></div><!--]--></div></div></div></div></div><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><!--[--><!--]--><div style="--borderRadius:10px;--opacity:1;--fontColor:#2c3e50;--fontFamily:-apple-system;--fitter-blue:1px;" class="sidebar-single-enter-animate footer-layout-center" data-v-4c868354><div class="footer box" id="footer" data-v-4c868354><!--[--><div class="footer-item" id="footer-item" data-v-4c868354><span>Copyright © by sakura All Rights Reserved.</span></div><div class="footer-item" id="footer-item" data-v-4c868354><span><a target='_blank' href='#' style='display:inline-block;text-decoration:none;height:20px;line-height:20px;'><img src='' style='float:left;'/><p style='float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px;'>💗</p></a></span></div><!--]--><div class="footer-item" id="footer-item" data-v-4c868354><span data-v-4c868354>小破站已运行207天22小时36分1秒</span></div></div></div></div><div id="set-bg" class="set-bg-fitter" style="--opacity: 1; --borderRadius: 10px; --backgroundImageUrl: url()"></div><div id="posterShade" class=""><span class=""></span></div></div><!----><!--]--><!----><div class="no-aurora-music-box aurora-music-box"><div class="music-box"><div class="no-show-more-song music-more"><!--[--><div class="aurora-music-more-single"><span class="aurora-music-song-active aurora-music-font aurora-music-music1 aurora-music-song-info aurora-music-cursor aurora-music-song">SAKURA</span></div><!--]--></div><div class="music-player"><div class="aurora-music-pic-par"><div class="aurora-music-pic" id="aurora-music-pic"><div class="aurora-music-pause"><span class="aurora-music-bofang4 aurora-music-font aurora-music-control-pause aurora-music-cursor"></span></div><img src="/sakura.png" alt=""></div></div><div class="aurora-music-info"><div class="aurora-music-info-left"></div><div class="aurora-music-info-right"><div class="aurora-music-title aurora-music-info-common"><span class="aurora-music-font aurora-music-music1 aurora-music-song-info aurora-music-cursor aurora-music-song">SAKURA</span></div><div class="aurora-music-bottom aurora-music-info-common"><div class="aurora-music-operate"><div><span class="aurora-music-font aurora-music-shangyishou1 aurora-music-cursor"></span></div></div><div class="aurora-music-operate"><div><span class="aurora-music-font aurora-music-xiayishou2 aurora-music-cursor"></span></div></div><div class="aurora-music-operate"><div><span class="aurora-music-geshunxubofang aurora-music-font aurora-music-cursor"></span></div></div><div class="aurora-music-operate"><div><span class="aurora-music-font aurora-music-gengduotianchong aurora-music-cursor aurora-music-operate-more"></span></div></div></div></div></div></div></div><audio autoplay class="aurora-music-player-control" controls="controls"><source src="musicSrc" type="audio/mp3"> Your browser does not support this audio format. </audio></div><div class="aurora-bubble" style="--aurora-bubble-z-index:-2;" data-v-267f56da><div class="aurora-bubble-box" id="aurora-bubble-box" data-v-267f56da></div><canvas class="aurora-bubble-canvas" id="aurora-bubble-canvas" data-v-267f56da></canvas></div><!--]--></div>
    <script src="/assets/js/runtime~app.5b846f34.js" defer></script><script src="/assets/js/452.9ec739fe.js" defer></script><script src="/assets/js/app.05eaf2e4.js" defer></script>
  </body>
</html>
